package com.example.gkcircle.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.avos.avoscloud.AVUser;
import com.example.gkcircle.bean.Wish;
import com.example.gkcircle.library.object.Book;
import com.example.gkcircle.library.object.DetailBook;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jehu on 16/4/8.
 */
public class DBUtil {


    private static DBUtil instance;
    private Context mContext;

    /** 数据库帮助类 **/
    private DBHelper dbHelper;

    public synchronized static DBUtil getInstance(Context context) {
        if (instance == null) {
            instance = new DBUtil(context);
        }
        return instance;
    }

    /**
     * 初始化
     *
     * @param context
     */
    private DBUtil(Context context) {
        dbHelper = new DBHelper(context);
        this.mContext=context;
    }


    /**
     * 关闭数据库
     */
    public static void destory() {
        if (instance != null) {
            instance.onDestory();
        }
    }

    /**
     * 关闭数据库
     */
    public void onDestory() {
        instance = null;
        if (dbHelper != null) {
            dbHelper.close();
            dbHelper = null;
        }
    }


    //心愿
    public void likeWish(Wish wish){
        ContentValues conv = new ContentValues();
        conv.put(DBHelper.WishTable.OBJECT_ID, wish.getObjectId());
        conv.put(DBHelper.WishTable.USER_ID, AVUser.getCurrentUser().getUsername());
        conv.put(DBHelper.WishTable.IS_LIKE, 1);
        dbHelper.insert(DBHelper.WISH_TABLE_NAME, null, conv);
        onDestory();
    }

    //获取心愿
    public boolean getLikeWish(Wish wish){
        if(null==AVUser.getCurrentUser()){
            return false;
        }
        Cursor cursor = null;
        String where = DBHelper.WishTable.OBJECT_ID
                + " = '"
                + wish.getObjectId()
                + "' and "
                + DBHelper.WishTable.USER_ID
                + "= '"
                + AVUser.getCurrentUser().getUsername()
                + "'";
        cursor = dbHelper.query(DBHelper.WISH_TABLE_NAME, null, where, null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            if (cursor.getInt(cursor.getColumnIndex(DBHelper.WishTable.IS_LIKE)) == 1) {
                return true;
            }
            cursor.close();
        }
        return false;
    }

    //添加书架
    public void addBookToShelf(Book book,String imageUrl){
        ContentValues conv = new ContentValues();
        conv.put(DBHelper.BookshelfTable.USER_ID, AVUser.getCurrentUser().getUsername());
        conv.put(DBHelper.BookshelfTable.BOOK_NAME, book.getBookName());
        conv.put(DBHelper.BookshelfTable.BOOK_URL, book.getUrl());
        conv.put(DBHelper.BookshelfTable.AUTHOR, book.getAuthor());
        conv.put(DBHelper.BookshelfTable.PUBLISH, book.getPublish());
        conv.put(DBHelper.BookshelfTable.CALL_NUMBER, book.getCallNumber());
        conv.put(DBHelper.BookshelfTable.IMAGE_URL,imageUrl);
        dbHelper.insert(DBHelper.BOOKSHELF_TABLE_NAME, null, conv);
        onDestory();
    }

    //获取书架图书
    public List<DetailBook> getBookshelf(){
        if(null==AVUser.getCurrentUser()){
            return null;
        }
        Cursor cursor = null;
        String where =DBHelper.BookshelfTable.USER_ID
                + "= '"
                + AVUser.getCurrentUser().getUsername()
                + "'";
        try{
            cursor = dbHelper.query(DBHelper.BOOKSHELF_TABLE_NAME, null, where, null, null, null, null);
        }catch (Exception e){
            return null;
        }
        cursor = dbHelper.query(DBHelper.BOOKSHELF_TABLE_NAME, null, where, null, null, null, null);
        List<DetailBook> detailBookList=new ArrayList<DetailBook>();
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            while(!cursor.isAfterLast()){
                DetailBook detailBook=new DetailBook();
                detailBook.setBookName(cursor.getString(cursor.getColumnIndex(DBHelper.BookshelfTable.BOOK_NAME)));
                detailBook.setAuthor(cursor.getString(cursor.getColumnIndex(DBHelper.BookshelfTable.AUTHOR)));
                detailBook.setPublish(cursor.getString(cursor.getColumnIndex(DBHelper.BookshelfTable.PUBLISH)));
                detailBook.setSmallImgUrl(cursor.getString(cursor.getColumnIndex(DBHelper.BookshelfTable.IMAGE_URL)));
                detailBook.setUrl(cursor.getString(cursor.getColumnIndex(DBHelper.BookshelfTable.BOOK_URL)));
                detailBook.setCallNumber(cursor.getString(cursor.getColumnIndex(DBHelper.BookshelfTable.CALL_NUMBER)));
                detailBookList.add(detailBook);
                cursor.moveToNext();
            }
            cursor.close();
        }
        return detailBookList;
    }


}
